<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/14
 * Time: 10:44
 */

include_once '../tools/DbTools.php';
//判断是否登录的方法
function getLoginStatus(){
    //获取COOKIE
    $Token = $_COOKIE['Token'];
    //判断$Token是否为空,为空返回false
    if(empty($Token)){
        return false;
    }
    //判断SESSION是否为空,为空返回false
    if(empty($_SESSION[$Token])){
        return false;
    }
    //如果$Token和SEESION都不为空则true不改变
    return true;
}
//存钱方法与取钱方法所集合的函数
function moneyInOut($type,$CardId,$money){
    $moneyIn = 0;//定义为存钱金额
    $moneyOut = 0;//定义为取钱金额
    $realmoney = $money;//定义为存进或取出金额
    //判断$type,如果type为1则表示为存钱,type为2则表示为取钱
    if($type == 1){
        $moneyIn = $money;
    }else{
        $moneyOut = $money;
        $realmoney = -$realmoney;
    }
    //初始化数据库
    DbTools::InitDb();
    //mysql语句用于个更新数据库
    //为bankcard表更新存款取款后卡余额
    $sql = "update bankcard set CardMoney = CardMoney + $realmoney where CardId = ".$CardId;
    //判断,当$type为2表示取钱时所取金额不超过银行卡内所拥有的金额
    if($type == 2){
        $sql.=" and CardMoney >= $money";
    }
    //执行noSelect方法完成数据库更新
    $result = DbTools::noSelect($sql);
    //判断$result数组
    if(!$result['status']){
        //关闭数据库连接
        DbTools::close();
        return $result;
    }
    //mysql语句用于添加数据库记录
    //为cardexchange表添加记录用于记录存款取款
    $sql = "insert into cardexchange(CardId,MoneyInBank,MoneyOutBank,ExchangeTime) 
            values ($CardId,$moneyIn,$moneyOut,NOW())";
    //执行noSelect方法完成数据库记录添加
    $result = DbTools::noSelect($sql);
    //关闭数据库连接
    DbTools::close();
    //返回结果$result
    return $result;
}
//根据卡号获取卡ID
function getCardNoByCardId($CardId){
    //初始化数据库
    DbTools::InitDb();
    //mysql语句用于查询
    //通过查询卡号以获取卡ID
    $sql = "select CardId from bankcard where CardNo = ".$CardId;
    //执行select方法查询所需
    $result = DbTools::select($sql);
    //关闭数据库连接
    DbTools::close();
    //判断$result数组是否为空
    if(!empty($result)){
        //$result数组不为空则返还所获取到的CardId
        return $result[0]['CardId'];
    }
    return 0;
}
//转账方法
function transferMoney($CardOut,$CardIn,$money,$type=0){
    //初始化数据库
    if($type==0){
        DbTools::InitDb();
    }
    //mysql语句用于更新数据库
    //更新转出账户金额,转出账户余额减去转出金额(减钱)
    $sql = "update bankcard set CardMoney = CardMoney - $money where CardId = $CardOut and CardMoney >= $money";
    //执行noSelect方法完成数据库更新
    $result = DbTools::noSelect($sql);
    //判断$result数组
    if(!$result['status']){
        //关闭数据库连接
        if($type==0){
            DbTools::close();
        }
        return $result;
    }
    //mysql语句用于更新数据库
    //更新转入账户金额,转入账户余额加上转入金额(加钱)
    $sql = "update bankcard set CardMoney = CardMoney - $money where CardId = $CardIn";
    //执行noSelect方法用于完成更新记录
    $result = DbTools::noSelect($sql);
    //判断$result数组
    if(!$result['status']){
        //关闭数据库连接
        if($type==0){
            DbTools::close();
        }
        return $result;
    }
    //mysql语句用于添加数据库记录
    //为cardtransfer表添加记录用于记录转账
    $sql = "insert into cardtransfer(CardIdOut,CardIdIn,TransferMoney,TransferTime) 
            values ($CardOut,$CardIn,$money,NOW())";
    //执行noSelect方法用于完成数据库记录添加
    $result = DbTools::noSelect($sql);
    //关闭数据库连接
    if($type==0){
        DbTools::close();
    }
    return $result;
}